package com.seashop.controller;

import com.seashop.request.UserBaseRequestParam;
import com.seashop.response.MyIncomeResponse;
import com.seashop.response.ResponseBase;
import com.seashop.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @author moredln
 * @desc 佣金相关
 * @date 2020/12/2 9:43 上午
 */
@RestController
@Api(tags = "会员中心")
@Slf4j
@Validated
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    @GetMapping("/queryMyIncome")
    @ApiOperation(value = "查询个人收益")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID", name = "userId", dataType = "integer", required = true),
            @ApiImplicitParam(value = "国家编码(TH:泰国;IN:印尼;ZH:中国;US:美国;UR:巴基斯坦;)", name = "countryCode", dataType = "string", required = true)
    })
    public ResponseBase<MyIncomeResponse> queryMyIncome(@NotBlank Integer userId, @NotBlank String countryCode) {
        MyIncomeResponse response = new MyIncomeResponse();
        BigDecimal freezeIncome = memberService.queryFreezeIncome(userId, countryCode);
        BigDecimal historyIncome = memberService.queryHistoryIncome(userId, countryCode);
        // if 0 income history (most of members)
        if (freezeIncome.equals(BigDecimal.ZERO) && historyIncome.equals(BigDecimal.ZERO)) {
            response.setExpectedIncome(BigDecimal.ZERO);
            response.setHistoryIncome(BigDecimal.ZERO);
            response.setTodayExpectedIncome(BigDecimal.ZERO);
            response.setMonthExpectedIncome(BigDecimal.ZERO);
            return ResponseBase.success(response);
        }
        LocalDate today = LocalDate.now();
        BigDecimal todayExpectedIncome = memberService.queryDailyFreezeIncome(userId, countryCode, today);
        BigDecimal monthExpectedIncome = memberService.queryMonthlyFreezeIncome(userId, countryCode, today);
        // 预期收益
        response.setExpectedIncome(freezeIncome);
        // 历史收益
        response.setHistoryIncome(historyIncome);
        // 今日预期收益
        response.setTodayExpectedIncome(todayExpectedIncome);
        // 当月预期收益
        response.setMonthExpectedIncome(monthExpectedIncome);
        return ResponseBase.success();
    }

    @GetMapping("/listOwnCommissionOrderPage")
    @ApiOperation(value = "分页查询个人消费收益订单列表")
    public ResponseBase listOwnCommissionOrderPage(UserBaseRequestParam param) {

        return ResponseBase.success();
    }


    @GetMapping("/listTeamCommissionOrderPage")
    @ApiOperation(value = "分页查询团队消费收益订单列表")
    public ResponseBase listTeamCommissionOrderPage(UserBaseRequestParam param) {

        return ResponseBase.success();
    }

    @GetMapping("/listTeamMemberPage")
    @ApiOperation(value = "分页查询团队会员列表")
    public ResponseBase listTeamMemberPage(UserBaseRequestParam param) {
        return ResponseBase.success();
    }

}
